home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch2 / MMeta.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-03-24  |  5.4 KB  |  174 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMMeta 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "MMeta"
  5.    ClientHeight    =   3495
  6.    ClientLeft      =   1950
  7.    ClientTop       =   825
  8.    ClientWidth     =   5295
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   3495
  12.    ScaleWidth      =   5295
  13.    Begin VB.CommandButton cmdClear 
  14.       Caption         =   "Clear"
  15.       Height          =   495
  16.       Left            =   3600
  17.       TabIndex        =   5
  18.       Top             =   240
  19.       Width           =   1215
  20.    End
  21.    Begin VB.PictureBox picCopy 
  22.       AutoRedraw      =   -1  'True
  23.       Height          =   1695
  24.       Index           =   2
  25.       Left            =   3600
  26.       ScaleHeight     =   1635
  27.       ScaleWidth      =   1635
  28.       TabIndex        =   4
  29.       Top             =   1800
  30.       Width           =   1695
  31.    End
  32.    Begin VB.PictureBox picCopy 
  33.       AutoRedraw      =   -1  'True
  34.       Height          =   1695
  35.       Index           =   1
  36.       Left            =   1800
  37.       ScaleHeight     =   1635
  38.       ScaleWidth      =   1635
  39.       TabIndex        =   3
  40.       Top             =   1800
  41.       Width           =   1695
  42.    End
  43.    Begin VB.PictureBox picCopy 
  44.       AutoRedraw      =   -1  'True
  45.       Height          =   1695
  46.       Index           =   0
  47.       Left            =   0
  48.       ScaleHeight     =   1635
  49.       ScaleWidth      =   1635
  50.       TabIndex        =   2
  51.       Top             =   1800
  52.       Width           =   1695
  53.    End
  54.    Begin VB.CommandButton cmdCopy 
  55.       Caption         =   "Copy"
  56.       Height          =   495
  57.       Left            =   3600
  58.       TabIndex        =   1
  59.       Top             =   960
  60.       Width           =   1215
  61.    End
  62.    Begin VB.PictureBox picSource 
  63.       AutoRedraw      =   -1  'True
  64.       Height          =   1695
  65.       Left            =   1800
  66.       ScaleHeight     =   109
  67.       ScaleMode       =   3  'Pixel
  68.       ScaleWidth      =   109
  69.       TabIndex        =   0
  70.       Top             =   0
  71.       Width           =   1695
  72.    End
  73. Attribute VB_Name = "frmMMeta"
  74. Attribute VB_GlobalNameSpace = False
  75. Attribute VB_Creatable = False
  76. Attribute VB_PredeclaredId = True
  77. Attribute VB_Exposed = False
  78. Option Explicit
  79. Private Drawing As Boolean
  80. Private PointX() As Single
  81. Private PointY() As Single
  82. Private NumPoints As Integer
  83. Private Declare Function CreateMetaFile Lib "gdi32" Alias "CreateMetaFileA" (ByVal lpString As Any) As Long
  84. Private Declare Function CloseMetaFile Lib "gdi32" (ByVal hMF As Long) As Long
  85. Private Declare Function PlayMetaFile Lib "gdi32" (ByVal hdc As Long, ByVal hMF As Long) As Long
  86. Private Declare Function DeleteMetaFile Lib "gdi32" (ByVal hMF As Long) As Long
  87. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
  88. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
  89. ' Create a memory metafile and play it back into
  90. ' the destination picture boxes.
  91. Private Sub cmdCopy_Click()
  92. Dim i As Integer
  93. Dim mDC As Long
  94. Dim hMF As Long
  95. Dim status As Long
  96. Dim x As Single
  97. Dim y As Single
  98.     ' Create the memory metafile.
  99.     mDC = CreateMetaFile(ByVal 0&)
  100.     If mDC = 0 Then
  101.         MsgBox "Error creating the metafile.", vbExclamation
  102.         Exit Sub
  103.     End If
  104.     ' Draw in the metafile.
  105.     For i = 1 To NumPoints
  106.         x = PointX(i)
  107.         y = PointY(i)
  108.         If x < 0 Then
  109.             MoveToEx mDC, -x, y, ByVal 0&
  110.         Else
  111.             LineTo mDC, x, y
  112.         End If
  113.     Next i
  114.     ' Close the metafile.
  115.     hMF = CloseMetaFile(mDC)
  116.     If hMF = 0 Then
  117.         MsgBox "Error closing the metafile.", vbExclamation
  118.     Else
  119.         ' Play the metafile.
  120.         For i = 0 To 2
  121.             picCopy(i).Cls
  122.             If PlayMetaFile(picCopy(i).hdc, hMF) = 0 Then
  123.                 MsgBox "Error playing the metafile.", vbExclamation
  124.                 Exit For
  125.             End If
  126.             picCopy(i).Refresh
  127.         Next i
  128.     End If
  129.     ' Delete the metafile.
  130.     If DeleteMetaFile(hMF) = 0 Then
  131.         MsgBox "Error deleting the metafile.", vbExclamation
  132.     End If
  133. End Sub
  134. ' Start drawing.
  135. Private Sub picSource_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  136.     Drawing = True
  137.     AddPoint -x, y
  138. End Sub
  139. ' Add a point to the list of points.
  140. Private Sub AddPoint(ByVal x As Single, ByVal y As Single)
  141.     ' Add the new point.
  142.     NumPoints = NumPoints + 1
  143.     ReDim Preserve PointX(1 To NumPoints)
  144.     ReDim Preserve PointY(1 To NumPoints)
  145.     PointX(NumPoints) = x
  146.     PointY(NumPoints) = y
  147.     ' This represents the start of a new segment.
  148.     If x < 0 Then
  149.         picSource.CurrentX = -x
  150.         picSource.CurrentY = y
  151.     Else
  152.         picSource.Line -(x, y)
  153.     End If
  154. End Sub
  155. ' Continue drawing.
  156. Private Sub picSource_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  157.     ' Do nothing if we are not drawing.
  158.     If Not Drawing Then Exit Sub
  159.     ' Add the point.
  160.     AddPoint x, y
  161. End Sub
  162. ' Stop drawing.
  163. Private Sub picSource_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  164.     Drawing = False
  165. End Sub
  166. ' Clear the form.
  167. Private Sub cmdClear_Click()
  168.     picSource.Cls
  169.     NumPoints = 0
  170. End Sub
  171. Private Sub mnuFileExit_Click()
  172.     Unload Me
  173. End Sub
  174.